package lesson06;

import TaskOne.Wolf;
import com.sun.source.tree.ContinueTree;

import javax.naming.Context;
import javax.naming.Name;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 学生管理程序的界面
 */
public class StudentUI {
    private SuperArray ary = new SuperArray();
    private Scanner sc = new Scanner(System.in);
    public StudentUI(){
        ary.add(new Student("张飞","男",20));
        ary.add(new Student("关羽","男",25));
        ary.add(new Student("刘备","男",30));
        menu();
    }

    /**
     * 菜单界面
     */
    public void menu(){
        System.out.println("欢迎使用学生管理程序");
        while(true){
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出  6-搜索");
            int input=0;
            try {
                input = sc.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入有误");
                sc.next();
                continue;
            }

            if(input == 1){
                show();

            }else if(input == 2){
                add();

            }else if(input == 3){
                update();

            }else if(input == 4){
                remove();

            }else if(input == 5){
                return;
            }else if (input==6){
                search();

            }
        }
    }



    /**
     * 显示所有学生
     */
    public void show(){
        for(int i = 0 ;i < ary.size();i++){
            Student student = (Student)ary.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }

    /**
     * 增加学生
     */
    public void add(){
        System.out.println("请输入学生姓名");
        String name ="0";
        name.matches("^[\u4e00-\u9fa5]{2}$");
        try {
            name =sc.next();
        }catch(InputMismatchException e){
            System.out.println("输入有误");
            sc.next();
            return;

        }

        System.out.println("请输入学生性别");
        String gender="0";
        gender.matches("^[\u4e00男-\u9fa5女]{1}$");
        try {
            gender=sc.next();
        }catch (InputMismatchException e){
            System.out.println("输入有误");
            sc.next();
            return;
        }

        System.out.println("请输入学生年龄");
        int age=0;
        try {
            age = sc.nextInt();
        }catch (InputMismatchException e){
            System.out.println("输入有误");
            sc.next();
            return;
        }

        ary.add(new Student(name,gender,age));

    }


    /**
     * 修改学生
     */
    public void update(){
        System.out.println("请输入学生名字");
        String name = sc.next();
        name.matches("^[\u4e00-\u9fa5]{2}$");
        while (true) {
            for (int i = 0; i < ary.size(); i++) {
                Student student = (Student) ary.get(i);    //objk
                if (name.equals(student.getName())) {
                    System.out.println("请输入更改内容 1-名字 2-性别 3-年龄");
                    int mun = 0;
                    try {
                        mun = sc.nextInt();
                    } catch (InputMismatchException e) {
                        System.out.println("输入有误");
                        sc.next();
                        continue;
                    }
                    System.out.println("请输入");
                    if (mun == 1) {
                        String name1 = sc.next();
                        student.setName(name1);
                        System.out.println("修改成功");
                        return;

                    } else if (mun == 2) {
                        String gender1 = sc.next();
                        student.setGender(gender1);
                        System.out.println("修改成功");
                        return;
                    } else if (mun == 3) {
                        String age1 = sc.next();
                        student.setGender(age1);
                        System.out.println("修改成功");
                        return;
                    }
                }
            }
            System.out.println("没有这个学生");
        }


    }

    /**
     * 删除学生
     */
    public void remove(){
        System.out.println("请输入删除的学生姓名");
        String name=sc.next();
        name.matches("^[\u4e00-\u9fa5]{2}$");
        for (int i=0;i<ary.size();i++){
            Student student=(Student) ary.get(i);
            if (name.equals(student.getName())) {
                ary.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("输入错误");     //  字符串 比较 equals
    }

    /**
     * 查找学生
     */

    private void search() {
        while (true){
            System.out.println("请输入搜索学生 1-姓名,2-性别,3-年龄");
            int input = 0;
            try {
                input=sc.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入有误");
                sc.next();
                continue;
            }
            boolean is=false;
            if (input==1){
                System.out.println("请输入搜索名字");
                String name =sc.next();
                name.matches("^[\u4e00-\u9fa5]{2}$");
                for (int i=0; i<ary.size();i++){
                    Student student=(Student) ary.get(i);
                    if (student.getName().equals(name)){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        is=true;
                    }
                }
            }else if (input==2){
                System.out.println("请输入性别");
                String gender=sc.next();

                for (int i=0;i<ary.size();i++){
                    Student student=(Student) ary.get(i);
                    if (student.getGender().equals(gender)){
                        System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                        is=true;
                    }
                }
            }else if (input==3){
                System.out.println("请输入年龄");
                int age=0;
                try {
                    age=sc.nextInt();
                }catch (InputMismatchException e){
                    System.out.println("输入有误");
                    sc.next();
                    return;
                }
                for (int i=0;i<ary.size();i++){
                    Student student=(Student) ary.get(i);{
                        if (student.getAge()==age){
                            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                            is=true;
                        }
                    }
                }
            }else {

            }
            if (!is){
                System.out.println("查无此人");
            }
            return;
        }















    }



    public static void main(String[] args) {
        new StudentUI();
    }
}
